#encoding=utf-8
import sys
import os
import jieba as jb
import jieba.analyse

# KEYWORD_RATIO = 0.91
KEYWORD_COUNT_RATIO = 0.1

KEYWORD_RATIO = 0.91

def GetCutResult(text):
    result = jb.cut(text, cut_all=False)
    return ' '.join(result)

#'n','v','vd','vn','a','ad','an','d'
def KeywordExtraction(content, topK=3):
    topK = int(len(content) * KEYWORD_COUNT_RATIO)
    print(topK)
    cutResult = GetCutResult(content)
    tags = jieba.analyse.extract_tags(cutResult, topK, withWeight=False, allowPOS=('n','v','vd','vn','a','ad','an','d'))
    return (",".join(tags))

#小说细纲提取
def GetNovelOutline(fileName, topK):
    with open(fileName, mode='r', encoding='utf-8') as fp:
        lines = fp.readlines()
        content = ''
        for i in range(0, len(lines)):
            line = lines[i].strip()
            # 写入章节名
            if (line.find('第') != -1) and (line.find('章') != -1) and (len(line) < 20):
                print(content)
                #提取关键词
                result = KeywordExtraction(content, topK)
                print(line + '\n' + result)
                content = ''
            content = content + line


if __name__ == '__main__':
    print('begin!')
    file = 'G:\\novel\\求魔.txt'
    GetNovelOutline(file, topK = 10)
    # for file in os.listdir(filePath):
    #     topK = 10 #关键词
    #     print(file)
        # GetNovelOutline(file, topK)
    print('end!')
